package com.example.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import com.example.json.Contacts;
import com.example.pro1.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter{
	private Context context;
	private List<Contacts> list;
	private List<Bitmap> photo;
	private Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 1:
				
				break;
			}
		};
	};
	

	public MyAdapter(Context context, List<Contacts> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view=LayoutInflater.from(context).inflate(R.layout.list_json, null);
		ImageView iv_photo=(ImageView) view.findViewById(R.id.iv_photo);
		TextView tv_name=(TextView) view.findViewById(R.id.tv_name);
		TextView tv_phone=(TextView) view.findViewById(R.id.tv_phone);
		getPhoto();
		tv_name.setText(list.get(position).getName());
		tv_phone.setText(list.get(position).getPhone());
		return view;
	}

	private void getPhoto() {
		
		for(int i=0;i<list.size();i++){
			OkHttpClient client=new OkHttpClient();
			Request request=new Request.Builder()
										.url(list.get(i+1).getPhoto())
										.build();
			client.newCall(request).enqueue(new Callback() {
				
				@Override
				public void onResponse(Call arg0, Response arg1) throws IOException {
					InputStream byteStream = arg1.body().byteStream();
					Bitmap bitmap=BitmapFactory.decodeStream(byteStream);
					
				}
				
				@Override
				public void onFailure(Call arg0, IOException arg1) {
					
				}
			});
		}
	}
	
	

}
